#!/bin/bash -ilex
#要部署的项目HOME 路径
deployProjectName=tj_work_platform
deployProjectHome=/usr/mpsp/tj_work_platform_fortest
tomcatHome=/usr/mpsp/tj_work_platform_fortest/apache-tomcat-8.5.37


echo "项目名: $deployProjectName"
echo "项目部署路径: $deployProjectHome"
echo "TOMCAT 部署路径: $tomcatHome"
export LANG=zh.UTF-8
if test -z $deployProjectHome ; then  echo '项目部署路径未配置! 例如: /usr/mpsp/tj_work_platform_fortest' && exit 1; fi
if test -z $deployProjectName ; then echo '项目部署名未配置!,例如：pl-app-busi' && exit 1 ; fi
if test -z $tomcatHome ; then echo 'tomcat Home 路径未配置!  例如：/usr/mpsp/tj_work_platform_fortest/apache-tomcat-8.5.37' && exit 1 ; fi
if [ ! -d $deployProjectHome ];then echo "项目路径不存在，请先创建项目路径" && exit 1 ; fi
if [ ! -d $tomcatHome ];then echo "TOMCAT 服务器home路径不存在，请先配置tomcat" && exit 1; fi

Env=fortest
#操作类型-deploy部署，rollback 回滚
operType=deploy
#ssh 插件默认部署包路径
sshPluginDefaultDeployPath=/usr/mpsp/jenkinsTemp/$Project
packageProjectName=${Project}_${Env}.tar.gz
uploadProject=$deployProjectHome/upload
backProject=$deployProjectHome/back

#back function
backupProject(){
	#backOperation $1:deployProjectHome $2:deployProjectName $3:backProject
	echo "project backup start ..."
	cd $tomcatHome/webapps/
	datetime=`date +"%Y%m%d%H%M%S"`
	backProjectFileName=${deployProjectName}.${datetime}.tar.gz
	tar -zcf $backProjectFileName --exclude=log $deployProjectName
	mv $backProjectFileName $backProject
	echo "project backup finished, back file name : $backProjectFileName"
}

#deployFunction
deployProject(){
	cp -R $sshPluginDefaultDeployPath/$packageProjectName $uploadProject/
	cd $uploadProject/
	echo "===============解压替换文件  START ==================="
	tar -zxvf  $packageProjectName  -C  $tomcatHome/webapps/
	echo "===============解压替换文件  FINISH ==================="
}

#runProjectFunction
runProject(){
	echo "环境信息："
	echo `java -version`
	cd $tomcatHome/bin
	chmod +x *.sh
	pwd
	echo "=========重启TOMCAT START ============="
	TOMCAT_PID=`ps aux|grep $tomcatHome |grep -v "grep" | awk '{print $2}'`
	if [ $TOMCAT_PID -gt 0 ]; then
    	echo "停止TOMCAT 服务,PID: $TOMCAT_PID"
		kill -9 $TOMCAT_PID
	fi
    cd $tomcatHome/bin
    sh startup.sh 

	echo "=========重启TOMCAT FINISH ============="
    sleep 2s
    tail -70 $tomcatHome/logs/catalina.out	

    echo "可访问的路径如下：============================"
    host=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|grep "10.10"|awk '{print $2}'|tr -d "addr:"`
	cd $tomcatHome/conf
	for port in $(grep "HTTP" server.xml|awk 'BEGIN {FS="\" "} {print $1}'|awk -F"\"" '{print $2}' | grep -v '^$')
	do
		echo "http://${host}:${port}/${deployProjectName}"
	done
}

#判断项目路径是否存在
if [ ! -d $deployProjectHome ];then mkdir $deployProjectHome ; fi
#判断上传路径是否存在不存在则创建
if [ ! -d $uploadProject ];then mkdir $uploadProject ; fi
#判断备份路径是否存在不存在则创建
if [ ! -d $backProject ];then mkdir $backProject ; fi

if [ $operType == "deploy" ] 
then
	#1.back project;
	backupProject
	#2.deployProject
	deployProject
	#3.run Project
	runProject
elif [ $operType == "rollback"] 
then 
    echo "不支持操作"
	#1.rollback
	#rollbackProject
	#2.runProject
	#runProject
else 
	echo "不支持操作"
fi

echo "deploy finished"